<template>
  <!-- 播放列表 -->
  <el-drawer
    v-model="showList"
    :modal="false"
    :show-close="false"
    :destroy-on-close="true"
    class="playlist-drawer">
    <template #header>
      <div class="playlist-header">
        <h4>播放列表</h4>
        <div class="header-operation">
          <span>{{ songNum }}首歌曲</span>
          <!-- 关闭侧边栏 -->
          <span
            @click="showList = false"
            class="iconfont close-list"
            >&#xea84;</span
          >
          <!-- 删除全部歌曲 -->
          <span
            @click="deleteAll"
            class="iconfont delete-song"
            >&#xe718;</span
          >
        </div>
      </div>
    </template>
    <ul>
      <li
        v-for="(song, index) in songList"
        :key="song.name"
        class="song">
        <p class="song-name">{{ song.name }}</p>
        <p class="song-singer">{{ song.singer }}</p>
        <p class="song-time">
          {{ transformTime(song.time as string) }}
        </p>
        <div class="operate">
          <!-- 播放 -->
          <span
            v-show="!isPlay || current != index"
            @mousedown.prevent="listPlay(index)"
            class="iconfont"
            >&#xea6e;</span
          >
          <!-- 暂停 -->
          <span
            v-show="isPlay && current == index"
            @mousedown.prevent="isPlay = false"
            class="iconfont"
            >&#xea6d;</span
          >
          <!-- 删除 -->
          <span
            @mousedown.prevent="listDelete(index)"
            class="iconfont delete-song"
            >&#xe718;</span
          >
          <!-- 更多 -->
          <span class="iconfont">&#xe605;</span>
        </div>
      </li>
    </ul>
  </el-drawer>
</template>

<script lang="ts" setup>
import { nextTick } from 'vue';
import useFooterStore from '../store/footer';
import { storeToRefs } from 'pinia';
import { transformTime, getTheme } from '../utils/util';
const color = getTheme().get('fontColor');
const boxShadow = getTheme().get('shadow');
const bg = getTheme().get('background');

const footer = useFooterStore();
const { isPlay, showList, songList, current, songNum, playProcess, playTime } =
  storeToRefs(footer);
//在播放列表点击播放
const listPlay = (index: number) => {
  if (current.value != index) {
    current.value = index;
  } else {
    isPlay.value = true;
  }
};

//在播放列表删除歌曲
const listDelete = async (index: number) => {
  if (current.value == index) {
    if (isPlay.value) {
      isPlay.value = false;
    }
    playProcess.value = 0;
    playTime.value = 0;
    if (index == songNum.value - 1) {
      current.value = 0;
    }
    songList.value.splice(index, 1);
    await nextTick();
    isPlay.value = true;
  } else if (current.value < index) {
    songList.value.splice(index, 1);
  } else {
    const process = playProcess.value;
    const time = playTime.value;
    songList.value.splice(index, 1);
    current.value -= 1;
    await nextTick();
    isPlay.value = false;
    playProcess.value = process;
    playTime.value = time;
    await nextTick();
    isPlay.value = true;
  }
};

//列表删除全部歌曲
const deleteAll = async () => {
  if (isPlay.value) {
    isPlay.value = false;
  }
  playTime.value = 0;
  playProcess.value = 0;
  songList.value.splice(0);
};
</script>

<style lang="less">
@font-color: v-bind(color);
@shadow: v-bind(boxShadow);
@background: v-bind(bg);
@font-color-gray: #7b7b7b;
@font-color-green: #1ed2a9;
.playlist-drawer {
  height: 100%;
  width: 23% !important;
  overflow: auto;
  background: @background;
  box-shadow: @shadow;

  .iconfont {
    cursor: pointer;
  }

  .playlist-header {
    padding-left: 10px;
    h4 {
      color: @font-color;
      font-size: 20px;
      font-weight: 500;
      color: @font-color;
    }

    .header-operation {
      display: flex;
      align-items: center;
      span {
        font-size: 12px;
      }
      .delete-song {
        font-size: 21px;
      }
      .close-list {
        font-size: 14px;
        margin: 0 10px 0 200px;
      }
    }
  }

  .song {
    margin: 10px;
    position: relative;
    &:hover .song-time {
      opacity: 0;
    }
    &:hover .operate {
      opacity: 1;
    }
    .operate {
      opacity: 0;
      position: absolute;
      display: none;
      top: 0;
      width: 324px;
      height: 44px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      span {
        font-size: 18px;
        margin: 0 5px;
        color: @font-color;
        &:last-child {
          margin-right: 60px;
        }

        &:hover {
          color: @font-color-green;
        }
      }
      .delete-song {
        font-size: 22px;
      }
    }
    .song-name {
      font-size: 14px;
      margin-bottom: 5px;
      color: @font-color;
    }
    .song-singer,
    .song-time {
      font-size: 12px;
      color: @font-color-gray;
    }
    .song-time {
      position: absolute;
      right: 10px;
      bottom: 0;
    }
  }
  .el-drawer__header {
    margin-bottom: 20px;
  }
  .el-drawer__body {
    padding-top: 0;
    &::-webkit-scrollbar {
      display: none;
    }
  }
}
</style>
